package com.onpositive.datacollection.proxy;

import java.io.UnsupportedEncodingException;

import com.onpositive.data.AbstractReader;
import com.onpositive.data.IPropertyConstants;
import com.onpositive.data.IPropertyMetadata;
import com.onpositive.data.diskstorage.AbstractObjectCollection;
import com.onpositive.data.diskstorage.StoredMetadata;
import com.onpositive.data.serializer.readers.RemoteByteArrayReader;
import com.onpositive.data.serializer.readers.RemoteByteArrayReader.Downloader;
import com.onpositive.datamap.IDataPropertyValue;
import com.onpositive.datamap.core.IDataCollection;
import com.onpositive.datamap.core.IDataObject;
import com.onpositive.datamap.core.IImageObject;
import com.onpositive.datamap.core.impl.AbstractDataProperty;
import com.onpositive.datamap.core.impl.DataObject;
import com.onpositive.datamap.item.SingleDataObject;

public class RemoteObjectCollection extends AbstractObjectCollection {

	private IRemoteCollectionProxy proxy;
	private String id;
	
	
	@Override
	public void setValue(int num, IPropertyMetadata meta, Object value) {
		proxy.setValue(id,num,meta,value);
	}
	
	public RemoteObjectCollection(IRemoteCollectionProxy p, String id2,
			RemoteCollectionInfo inf) {
		super(inf.getSize());
		this.id = id2;
		for (IPropertyMetadata m : inf.getMetas()) {
			addMetadata(m);
			if (m.id().equals(IPropertyConstants.DOCUMENT_NAME_PROPERTY_METADATA_ID)){
				initLocal(m,inf.nameReader);
			}			
		}
		this.proxy = p;
		StoredMetadata m=new StoredMetadata();
		m.setId(IPropertyConstants.TEXT_CONTENT_PROPERTY_NAME);		
		addMetadata(m);
		m=new StoredMetadata();
		m.setId(IPropertyConstants.PREVIEW_PICTURE_PROPERTY_METADATA_ID);		
		addMetadata(m);
		m=new StoredMetadata();
		m.setId(IPropertyConstants.MAIN_PICTURE_PROPERTY_METADATA_ID);		
		addMetadata(m);
	}
	
	IDataCollection c;
	protected DataObject getDataObject(int i){
		return (DataObject) c.getObject(i);
	}
	@Override
	public void setValue(IDataObject obj, AbstractDataProperty p, Object value) {
		if (obj instanceof SingleDataObject){
			SingleDataObject m=(SingleDataObject) obj;
			m.setProperty(p, value);
		}
		super.setValue(obj, p, value);
		try{
			if (value instanceof IDataPropertyValue){
				IDataPropertyValue pq=(IDataPropertyValue) value;
				value=pq.clone(null);
			}
		proxy.putValue(id,obj.getUID(),p.getId(),value);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	protected AbstractReader<?> initReader(IPropertyMetadata meta) {
		if (meta.id().equals(IPropertyConstants.MAIN_PICTURE_PROPERTY_METADATA_ID)){
			RemoteByteArrayReader remoteByteArrayReader = new RemoteByteArrayReader(Object.class, null, new Downloader() {
				
				@Override
				public byte[] getContent(int i) {
					DataObject dataObject = getDataObject(i);
					IImageObject primaryImage = dataObject.getPrimaryImage();
					if(primaryImage!=null){
						return primaryImage.getBytes(-1, -1);
					}
					return null;
				}
			});
			recordReader(meta, remoteByteArrayReader);
			return remoteByteArrayReader;
		}
		if (meta.id().equals(IPropertyConstants.PREVIEW_PICTURE_PROPERTY_METADATA_ID)){
			RemoteByteArrayReader remoteByteArrayReader = new RemoteByteArrayReader(Object.class, null, new Downloader() {
				
				@Override
				public byte[] getContent(int i) {
					DataObject dataObject = getDataObject(i);
					IImageObject primaryImage = dataObject.getPreviewImage();
					if(primaryImage!=null){
						return primaryImage.getBytes(-1, -1);
					}
					return null;
				}
			});
			recordReader(meta, remoteByteArrayReader);
			return remoteByteArrayReader;
		}
		if (meta.id().equals(IPropertyConstants.TEXT_CONTENT_PROPERTY_NAME)){
			RemoteByteArrayReader remoteByteArrayReader = new RemoteByteArrayReader(Object.class, null, new Downloader() {
				
				@Override
				public byte[] getContent(int i) {
					DataObject dataObject = getDataObject(i);
					String textContentAsString = dataObject.getTextContentAsString();
					if (textContentAsString!=null){
					try {
						return textContentAsString.getBytes("UTF-8");
					} catch (UnsupportedEncodingException e) {
						throw new IllegalStateException();
					}
					}
					return null;
					
				}
			});
			recordReader(meta, remoteByteArrayReader);
			return remoteByteArrayReader;
		}
		byte[] bs = proxy.getPropertyData(id, meta.id());
		
		return initLocal(meta, bs);
	}
	@Override
	public void updateMeta(IPropertyMetadata mq) {
		proxy.updateMeta(id, mq);		
	}

}
